package com.slf.common.util;

import lombok.Data;

/**
 * @author slf
 * @date 2021/8/18
 */
@Data
public class Result<T> {
    private int code;
    private String msg;
    private T data;
    
    public Result(){}

    public Result(ResultCode rc) {
        this.code = rc.getCode();
        this.msg = rc.getMsg();
    }

    public static <T> Result<T> success(T data) {
        ResultCode aec = ResultCode.SUCCESS;
        if (data instanceof Boolean && Boolean.FALSE.equals(data)) {
            aec = ResultCode.FAILED;
        }
        return restResult(data, aec);
    }

    public static <T> Result<T> failed(String msg) {
        return restResult(null, ResultCode.FAILED.getCode(), msg);
    }

    public static <T> Result<T> failed(ResultCode resultCode) {
        return restResult(null, resultCode);
    }

    public static <T> Result<T> restResult(T data, ResultCode resultCode) {
        return restResult(data, resultCode.getCode(), resultCode.getMsg());
    }

    private static <T> Result<T> restResult(T data, int code, String msg) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }
}
